package main;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;
 
public class USBManager {

	
	static ConcurrentHashMap<String , File> currentFiles = new ConcurrentHashMap<String, File>();
	
	static final String[] extensionsAccpeted = {"wav"};
	

	public static Set<String> getAllFileNames() {
		return  currentFiles.keySet();
	}
	
	public static Collection<File>   getAllFiles() {
		return  currentFiles.values() ;
	}
	
	
	public static File nameToFile(String nameOfFile) {
		return currentFiles.get(nameOfFile);
		
	}
	
	public static void loadUSBContent() {
		for (File f  : UsbDiscover.getUSBWAVFiles()) {
			currentFiles.put(f.getName(), f);
		}
		
	}
	
	/**
	 * Loads all the wav files from the selected drive
	 * @param driveLetter The drive to look for the wav files
	 */
	public static void loadContentFromSelectedDrive(char driveLetter) {
	 
		for (File file  : UsbDiscover.getFilesMatchExtensionDriveFromPath(driveLetter + ":\\", extensionsAccpeted,
				new Integer(0))) {
			currentFiles.put(file.getName(), file);
		}
		
	}
	
	
	/**
	 * Removes specified file from local storage and updates
	 * Usbmanager to reflect change
	 * @param nameOfFileToRemove name of file to be removed 
	 * include extension but not path
	 */
		public static void removeFromUSB(String nameOfFileToRemove) {
		
			File referenceToDeleteFile  = currentFiles.remove(nameOfFileToRemove);
			referenceToDeleteFile .delete();
		}
	
		
 
	/**
	 * Changes the name of a file on local storage(usb, hard disk)
	 * updates internal map containing all files to reflect change
	 * @param oldName name to change from (include extension exclude path)
	 * @param newName name to  change to (include extension exclude path)
	 * @throws IOException 
	 */
	public static void renameFile(String oldName, String newName) throws IOException {
		
		
	File oldNameReference = currentFiles.remove(oldName);
	File newFileReference = new File(oldNameReference.getParent() + "\\" + newName);
 
//	 oldNameReference.renameTo(newFileReference) ;
//	currentFiles.put(newName, newFileReference);
	FileUtils.moveFile(oldNameReference,newFileReference);
	}
 
	public static void main (String args[]) {
		System.out.println(new File("I:\\ff.wav").renameTo(new File("I:\\tll.wav")));
	}
}
 